import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic

Page {
    id: pageImportWords

    property int selectedCategoryId: 0
    property string xmlfile: ""

    //property QtObject _txtTF


    tools: ToolBarLayout {
        id: tool_only_back
        opacity: userTheme.opacityToolbar
        ToolIcon {
            id: toolIconBack
            //iconId: "toolbar-back"
            iconSource: "images/toolbar/back.png"
            onClicked: {
                pageStack.pop();
            }
        }
    } //ToolBarLayout

    QNMTitle {
        id: titlePage
        text: qsTr("Import of words")
        image: "images/import.png"
        busy: false
    }

    BackgroundPage { }

    Flickable {
        id: flickArea
        anchors {
            top: titlePage.bottom
            bottom: parent.bottom
            left: parent.left
            right: parent.right
            topMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 5 : 15
            leftMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 40 : 20
            rightMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 40 : 20
            bottomMargin: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? 5 : 15
        }

        flickableDirection: Flickable.VerticalFlick

        width: parent.width
        height: parent.height

        contentWidth: contentFlickArea.width
        contentHeight: contentFlickArea.height
        clip: true

        Column {
            id: contentFlickArea
            width: parent.width
            spacing: 20

            Text {
                text: qsTr("Category")
                font.pixelSize: 30
            }
            MyButton {
                id: categorySelectButton
                iconSource: "images/categories_icon.png"
                text: qsTr("List of categories")
                fontSize: 24
                height: 100
                width: flickArea.width
                onClicked: {
                    dlgSelectCategory.open()
                }
            }

            Item {
                height: (screen.orientationString == "Portrait") || (screen.orientationString == "PortraitInverted") ? 20 : 5
                width:flickArea.width
            }

            Text {
                text: qsTr("XML file")
                font.pixelSize: 30
            }
            Row {
                TextField {
                    id: tfXmlFile
                    text: pageImportWords.xmlfile
                    height: 70
                    width: flickArea.width - btBrowse.width
                    anchors.verticalCenter: parent.verticalCenter
                    onTextChanged: {
                        pageImportWords.xmlfile = tfXmlFile.text
                        var res = cppMeeWords.checkFile( tfXmlFile.text )
                        if (res) {
                            btImport.enabled = true
                        } else {
                            btImport.enabled = false
                        }
                    }
                    /*Component.onCompleted: {
                        _txtTF = tfXmlFile
                    }*/
                }
                MyButton {
                    id: btBrowse
                    width: 80
                    height: 70
                    anchors.verticalCenter: parent.verticalCenter
                    iconSource: "images/folder_docs.png"
                    indicator: false
                    isGlow: false
                    onClicked: {
                        Logic.openFile("FileListView.qml")
                    }
                }
            }

            Item {
                height: (screen.orientationString == "Portrait") || (screen.orientationString == "PortraitInverted") ? 20 : 5
                width:flickArea.width
            }

            MyButton {
                id: btImport
                height: 100
                width: flickArea.width
                text: qsTr("Start the import")
                iconSource: "images/import.png"
                onClicked: {
                    createImport()
                }
                enabled: false
            }

            MyButton {
                id: btHelp
                height: 100
                width: flickArea.width
                text: qsTr("Help")
                //iconSource: "image://theme/icon-l-user-guide"
                iconSource: "images/info.png"
                onClicked: {
                    Logic.openFile("ImportHelp.qml")
                }
            }


        }

    } //Flickable


    /*******************************************************************************/
    function createImport() {
         var component = Qt.createComponent( "ImportWords.qml" );
         if ( component.status == Component.Ready ) {
             finishCreation( component );
         } else {
             component.statusChanged.connect( finishCreation );
         }
     }

     function finishCreation( component ) {
         if (component.status == Component.Ready) {
             var sprite = component.createObject(pageImportWords, { });
             if (sprite == null) {
                 // Error Handling
                 console.log("Error creating object");
             }
         } else if (component.status == Component.Error) {
             // Error Handling
             console.log("Error loading component:", component.errorString());
         }
     }

    function getAllCategories () {
        titlePage.busy = false
        categorySelectButton.enabled = false
        var db = Logic.getDatabase()

        listModelCategory.clear()

        db.transaction( function(tx) {
                           var res = tx.executeSql( "SELECT id,category FROM categories" )
                           for(var i = 0; i<=res.rows.length-1; i++) {
                               categorySelectButton.enabled = true
                               var _id = res.rows.item(i).id;
                               var nameCategory = res.rows.item(i).category;
                               //var descrCategory = res.rows.item(i).description;
                               listModelCategory.append( { name: nameCategory, category_id: _id } );
                           }
                       }
                    )
        /* установка значение для первого элемента */
        titlePage.busy = false
    }

    /*******************************************************************************/


    ListModel {
        id: listModelCategory
        Component.onCompleted: {
            getAllCategories()
        }
    }

    SelectionDialog {
        id: dlgSelectCategory
        titleText: qsTr("Select category")
        model: listModelCategory
        onSelectedIndexChanged: {
            var j = selectedIndex
            pageImportWords.selectedCategoryId = listModelCategory.get(j).category_id
            var nc = listModelCategory.get(j).name
            categorySelectButton.text = nc
        }
    }
}
